<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 * 客户信息db管理
 */

require_once(dirname(__FILE__) . "/../common/EasyMysql.php");
require_once(dirname(__FILE__) . "/../config/ConfigDB.php");

class OrderCom {

    function __construct() {
        $dbconfig = $GLOBALS["config"] ['database'];
        $mysql = new EasyMysql($dbconfig['hostname'], $dbconfig ['port'], $dbconfig ['username'], $dbconfig ['password'], $dbconfig ['db'], $dbconfig ['charset']);
        $this->mysql = $mysql;
    }

    function getOrderlist($customer_id) {
        try {
            $sql = "select * from `order` where customer_id={$customer_id} order by id DESC";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }

    function getOrderById($id) {
        try {
            $sql = "select * from `order` where id={$id}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }

    function addStaisById($orderid, $staisid) {
        try {
            $sql = "update appoint set stais_id={$staisid} where id={$orderid}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getOrderByTradeno($tradeno){
        try {
            $sql = "select * from `order` where trade_no='{$tradeno}'";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function checkOrderTime($time, $communityid, $reservetime_id) {
        try {
            $sql = "select id from `order` where time='{$time}' and reservetime_id={$reservetime_id} and community_id={$communityid}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }

    function addOrder($customerid,$tradeno, $time, $totalfee) {
        try {
            $create_time = time();
            $sql = "insert into `order` (customer_id,trade_no,totalfee,create_time)"
                    . " values({$customerid},'{$tradeno}',{$totalfee},'{$create_time}');SELECT @@Identity";
            $ret = $this->mysql->query($sql);
            return $ret[0][0]['@@Identity'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function changeOrderisPay($orderid, $ispay) {
        try {
            $sql = "update `order` set is_pay={$ispay} where id={$orderid}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getOrderlistByComId($community_id){
        try {
            $sql = "select * from `order` where community_id={$community_id}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getAllOrderByCid($customerid,$minlimit, $limitnum){
        try{
            $sql = "select * from `order` where customer_id={$customerid} `order` by id DESC limit {$minlimit},{$limitnum}";
            $ret = $this->mysql->query($sql);
            if(!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])){
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getAllOrdermByCid($customerid){
        try{
            $sql = "select id from `order` where customer_id={$customerid}";
            $ret = $this->mysql->query($sql);
            if(!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])){
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getAllNumByCid($customerid){
        try{
            $sql = "select count(1) from `order` where customer_id={$customerid}";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getAllOrder($minlimit, $limitnum){
        try{
            $sql = "select * from `order` order by id DESC limit {$minlimit},{$limitnum}";
            $ret = $this->mysql->query($sql);
            if(!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])){
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getAllNum(){
        try{
            $sql = "select count(1) from `order`";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getAllwOrder($community,$minlimit, $limitnum){
        try{
            $sql = "select * from `order` where community_id in ({$community}) order by id DESC limit {$minlimit},{$limitnum}";
            $ret = $this->mysql->query($sql);
            if(!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])){
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getAllwNum($community){
        try{
            $sql = "select count(1) from `order` where community_id in ({$community})";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function deleteOrder($id){
        try{
            $sql = "delete from `order` where id={$id}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getNumByAge(){
        try{
            $sql = "select age,count(1) from `order` group by age";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumwByAge($community){
        try{
            $sql = "select age,count(1) from `order` where community_id in ({$community}) group by age";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumByFloor(){
        try{
            $sql = "select floor,count(1) from `order` group by floor";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumwByFloor($community){
        try{
            $sql = "select floor,count(1) from `order` where community_id in ({$community}) group by floor";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumBySex(){
        try{
            $sql = "select sex,count(1) from `order` group by sex";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    function getNumwBySex($community){
        try{
            $sql = "select sex,count(1) from `order` where community_id in ({$community}) group by sex";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    function getNumByArea($area){
        try{
            $sql = "select count(1) from `order` where area_id={$area}";
            $ret = $this->mysql->query($sql);
            return $ret [0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    function getNumByUsertype($usertype){
        try{
            $sql = "select count(1) from `order` where usertype_id={$usertype}";
            $ret = $this->mysql->query($sql);
            return $ret [0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    function getNumwByUsertype($usertype,$community){
        try{
            $sql = "select count(1) from `order` where usertype_id={$usertype} and community_id in ({$community})";
            $ret = $this->mysql->query($sql);
            return $ret [0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    function getOrderidByCid($communityid){
        try{
            $sql = "select id from `order` where community_id={$communityid}";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    function getOrderHistory($customerid){
        try{
            $sql = "select * from `order` where customer_id={$customerid} order by id DESC";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
}
